import { addTag, getTag, updateTag, delTag } from 'api/tag'

const tag = {
  state: {
    tagList: [],
    tagTotal: 0
  },
  mutations: {
    SET_TAGLIST(state, data) {
      state.tagList = data.list
      state.tagTotal = data.total
    }
  },
  actions: {
    GetTagList({ commit }, params) {
      return new Promise((resolve, reject) => {
        getTag(params).then(response => {
          commit('SET_TAGLIST', response.data)
          resolve(response.data)
        }).catch(err => {
          reject(err)
        })
      })
    },
    AddTag({ commit }, params) {
      return new Promise((resolve, reject) => {
        addTag(params).then(response => {
          resolve(response)
        }).catch(err => {
          reject(err)
        })
      })
    },
    UpdateTag({ commit }, params) {
      const id = params.id
      delete params.id
      return new Promise((resolve, reject) => {
        updateTag(id, params).then(response => {
          resolve(response)
        }).catch(err => {
          reject(err)
        })
      })
    },
    DelTag({ commit }, id) {
      return new Promise((resolve, reject) => {
        delTag(id).then(response => {
          resolve(response)
        }).catch(err => {
          reject(err)
        })
      })
    }
  }
}
export default tag
